<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="doAdd('0',1)">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button v-if="scope.row.type===1" type="text" @click="doAdd(scope.row.id,2)">添加</el-button>
              <el-button type="text" @click="doEdit(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="doDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

      </el-card>
    </div>
    <!-- 添加弹窗 -->
    <el-dialog :visible.sync="showDialog" :title="isEdit? '编辑':'新增'" @close="doClose">
      <!-- 表单内容 -->
      <el-form ref="formDataRef" label-width="100px" :model="formData" :rules="formDataRules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="doSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>
<script>
import { getPermissionList, addPermission, updatePermission, delPermission, getPermissionDetail } from '@/api/permissions'
import { arrayToTree } from '@/utils/ArrayToTree'
export default {
  data() {
    return {
      // 权限列表
      list: [],
      // 弹窗表单数据
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: 0,
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      // 是否显示弹层
      showDialog: false,
      // 表单校验规则
      formDataRules: {
        name: [
          { required: true, message: '名称不能为空', tirgger: 'blur' }
        ],
        code: [
          { required: true, message: '标识不能为空', tirgger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', tirgger: 'blur' }
        ]
      },
      // 判断是否是编辑弹窗
      isEdit: false
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 打开页面加载权限信息
    async loadPermissionList() {
      const { data: res } = await getPermissionList()
      // console.log('权限信息列表', res)
      // this.list = res.data
      this.list = arrayToTree(res.data, '0')
    },
    // 添加权限
    doAdd(pid, type) {
      this.isEdit = false
      this.showDialog = true
      this.formData.pid = pid
      this.formData.type = type
    },
    // 提交表单
    doSubmit() {
      this.$refs.formDataRef.validate(async(valid) => {
        if (valid) {
          // 判断是否是编辑弹窗
          if (this.isEdit) {
            // 发送更新请求
            const { data: res } = await updatePermission(this.formData)
            // console.log('更新权限信息', res)
            // 提示信息
            this.$message.success(res.message)
            // 关闭弹窗
            this.showDialog = false
            // 刷新列表
            this.loadPermissionList()
          } else {
            // 发送请求
            const { data: res } = await addPermission(this.formData)
            // console.log('新增权限', res)
            // 提示信息
            this.$message.success(res.message)
            // 关闭弹窗
            this.showDialog = false
            // 刷新列表
            this.loadPermissionList()
          }
        }
      })
    },
    // 删除权限
    doDel(id) {
      this.$confirm('是否删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 发送请求
        const { data: res } = await delPermission(id)
        // console.log('删除员工', res)
        // 提示信息
        this.$message.success(res.message)
        // 刷新列表
        this.loadPermissionList()
      }).catch(() => {
      })
    },
    // 编辑权限
    async doEdit(id) {
      this.showDialog = true
      this.isEdit = true
      // 获取id详情
      const { data: res } = await getPermissionDetail(id)
      // sconsole.log('获取id详情', res)
      // 将数据回填到表单中
      this.formData = { ...res.data }
    },
    // 重置表单
    doClose() {
      // 清空表单
      this.formData = {
        name: '',
        code: '',
        description: ''
      }
      this.$refs.formDataRef.resetFields()
    }
  }
}
</script>
